const Router = require("koa-router");

const user = require("../control/user");

const admin = require("../control/admin");

const upload = require("../utils/upload");

const userManagement = require("../control/userManagement");

const quesManagement = require("../control/questionManagement");

const router = new Router;

// 监听根目录，返回登录页面
router.get('/', async ctx => {
    ctx.redirect("/admin/login")
});

// 主要用来处理返回  用户登录 用户注册
router.get("/admin/login", async ctx => {
    await ctx.render("login")
});

// 处理管理员登录
router.post('/admin/login', admin.login);

// 处理管理员修改密码
router.get("/admin/views/password", async ctx => {
    await ctx.render("/admin/password")
});

// 处理登录成功跳转
router.get('/admin/views', async ctx => {
    await ctx.render("admin/admin", {
        session: ctx.session
    })
});

// 管理员退出
router.get("/admin/logout", admin.logout);

// 返回 文件 上传界面
router.get('/admin/views/userAdd', async ctx => {
    await ctx.render("admin/userAdd")
});

// 返回 用户管理界面
router.get('/admin/views/userManagement', async ctx => {
    await ctx.render('admin/userManagement')
});

// 获取用户列表
router.get('/admin/views/userManagement/userList', userManagement.getUserList);

// 处理 用户数据更新
router.post('/admin/views/userManagement/update', userManagement.update);

// 处理 用户删除
router.post('/admin/views/userManagement/delete', userManagement.delete);

// 处理用户登录的 post 
router.post('/user/login', user.keepLog, user.login);

// 用户获取试题数据
router.get('/user/ques', user.quesList);

// 情绪分析
router.get('/user/analysis', user.analysis);

// 用户试题答案
router.post('/user/quesAns', user.quesAns);

// 用户试题记录
router.get('/user/history', user.history);

// 处理试题 post 上传 Excel 文件
router.post('/admin/views/userUpload', upload.single('file'), admin.userUpload);

// 处理试题 post 上传 Excel 文件
router.post('/admin/views/questionUpload', upload.single('file'), admin.questionUpload);

// 返回 题库管理界面
router.get('/admin/views/quesManagement', async ctx => {
    await ctx.render("admin/questionManagement")
});

// 获取题库列表
router.get('/admin/views/quesManagement/quesList', quesManagement.getQuesList);

// 处理 试题删除
router.post('/admin/views/quesManagement/delete', quesManagement.delete);

// 处理 试题数据更新
router.post('/admin/views/quesManagement/update', quesManagement.update);

// 返回 试题查看
router.get('/admin/views/quesManagement/quesList/quesView', async ctx => {
    await ctx.render("admin/quesView")
});

// 返回 试题上传界面
router.get('/admin/views/quesAdd', async ctx => {
    await ctx.render('admin/questionsAdd')
});

module.exports = router;


